{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Robot World 1-D\n",
    "\n",
    "### Introduction\n",
    "In the nanodegree, you are going to see robot probability distributions represented two different ways: \n",
    "* with discrete probability distributions\n",
    "* and with continuous probability distributions\n",
    "\n",
    "Discrete probability distributions are used when you track a robot's movement across a map divided into square grids. Each grid is a discrete location where the robot could be located.\n",
    "\n",
    "In this exercise, you'll work with a probability distribution representing the uncertainty in a robot's location."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Robot Initialization\n",
    "Imagine you have a robot living in a 1-D world. The robot lives on a grid with nine different spaces. The robot can only move forwards or backwards. If the robot falls off the grid, it will loop back around to the other side.\n",
    "\n",
    "The robot has a map so that it knows there are only nine spaces. But the robot does not know where it is on the map. Here is the 1-D map.\n",
    "\n",
    "![1-D Robot World](1D_world.jpg \"1-D Robot World\")\n",
    "\n",
    "When the robot first turns on, the probability that the robot is on any one of these spaces is 1/9; the implication is that the robot has an equal probability of being at any of the spaces on the grid.\n",
    "\n",
    "\n",
    "# Exercise 1 - Initial Probability\n",
    "\n",
    "Now, write a function that receives the number of spaces in the robot's world and then returns a list containing the initial probability for each space on the grid.\n",
    "\n",
    "So in the example given so far, there would be a list with nine probabilities. And every value in the list would be 1/9. Remember, because the robot does not know where it is at first, the probability of being in any space is the same.\n",
    "\n",
    "Python's list.append() method might be useful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "def initialize_robot(grid_size):\n",
    "    \n",
    "    grid = []\n",
    "    \n",
    "    # TODO: for each space on the map grid, store the initial probability\n",
    "    # in the grid list. For example, if there are eight spaces on the grid,\n",
    "    # the grid list should have eight entries where each entry represents\n",
    "    # the initial probability of the robot being in that space.\n",
    "    \n",
    "    for i in range(grid_size):\n",
    "        grid.append( 1/grid_size )\n",
    "    \n",
    "    return grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below to make sure that your function outputs the correct results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hooray! You just initialized a discrete probability distribution\n"
     ]
    }
   ],
   "source": [
    "# Result should be a list with 9 elements all having value 1/9\n",
    "assert initialize_robot(9) == [1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9]\n",
    "\n",
    "# Result should be a list with 4 elements all having value 1/4\n",
    "assert initialize_robot(4) == [1/4, 1/4, 1/4, 1/4]\n",
    "\n",
    "print('Hooray! You just initialized a discrete probability distribution')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 2 - Grid Probability\n",
    "\n",
    "Now, write a function called `grid_probability` that outputs the probability that the robot is at a specific point on the grid. The input to this function will be:\n",
    "\n",
    "* the output of the previous function (ie a list representing a 1-D map of probabilities)\n",
    "* the grid number where you want to know the probability\n",
    "\n",
    "So if you wanted to know the current probability that the robot is at the fifth tile on the grid, you would called the function like:\n",
    "\n",
    "`grid_probability(my_grid, 4)`.\n",
    "\n",
    "Why would the function input be 4 instead of 5? Think about how Python accesses values in a list. Typing `mylist[0]` gives you the first element in the list. Typing `mylist[1]` gives you the second element in the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grid_probability(grid, position):\n",
    "    \n",
    "    ##### \n",
    "    # TODO: Use an if statement to make sure that the position input\n",
    "    # does not go beyond the size of the list. Say the list has five elements\n",
    "    # and your code tries to access grid[5] or grid[6]. That will lead to an \n",
    "    # error. \n",
    "    if position - 1 > len(grid):\n",
    "        print(\"beyond the size of the grid\")\n",
    "        return None\n",
    "    \n",
    "    # TODO: If the position input is legitimate, then return the probability\n",
    "    # stored at that position. If the position input is not legitimate, then\n",
    "    # return None\n",
    "    #####\n",
    "    \n",
    "    return grid[position]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below to test the results of your code. If the grid_probability function works as expected, the code cell should print out \"Awesome work!\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "beyond the size of the grid\n",
      "Awesome work!\n"
     ]
    }
   ],
   "source": [
    "assert grid_probability([.1, .1, .2, .1, .5], 2) == 0.2\n",
    "    \n",
    "assert grid_probability([.1, .1, .2, .1, .5], 7) == None\n",
    "\n",
    "print('Awesome work!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 3 - Visualize Robot World\n",
    "\n",
    "Next, write a function that outputs a bar chart showing the probabilities of each grid space. \n",
    "\n",
    "Remember that we are working with a discrete probability distribution; the robot location can only take on certain values ie square 1, square 2, square 3, square 4, etc. \n",
    "\n",
    "The grid number would be on the x-axis. For a discrete probability distribution, the y-axis represents probability.\n",
    "\n",
    "The input to the function is a list with the probability that the robot is at each point on the grid.\n",
    "\n",
    "Your result should look something like this:\n",
    "\n",
    "![Uniform Probability Distribution](uniform_prob.png \"Uniform Probability Distribution\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "def output_map(grid):\n",
    "    \n",
    "    ###\n",
    "    # TODO: Start by creating a list to represent the x-axis labels.\n",
    "    # For example, if the grid variable has length 5, x_labels would contain\n",
    "    # a list [0, 1, 2, 3, 4]. \n",
    "    #\n",
    "    # HINT: Python's built in len() and range() functions might\n",
    "    #       be useful. If you are not sure how to use these\n",
    "    #       functions, look them up in a search engine.\n",
    "    #       For example, in google, look up \"Python len\".\n",
    "    ###\n",
    "    \n",
    "#     x_labels = [i for i in range(len(grid))]\n",
    "    x = [i for i in range(len(grid))]\n",
    "#     print(x)\n",
    "    y = [i for i in grid]\n",
    "#     print(y)\n",
    "\n",
    "    plt.bar(x, y)\n",
    "    plt.xlabel('Grid Space')\n",
    "    plt.ylabel('Proba')\n",
    "    plt.title('123')\n",
    "#     plt.xticks(rotation=70)\n",
    "    plt.show()\n",
    "    \n",
    "    ###\n",
    "    # TODO: Using matplotlib, output a bar chart of the results.\n",
    "    # Notice that we have already imported the matplotlib library\n",
    "    # at the top of this code cell.\n",
    "    \n",
    "    # If you are unsure how to make a bar chart, go back to the \n",
    "    # \"Plotting in Python demonstration\" to see an example.\n",
    "    \n",
    "    # Make sure your plot has an xlabel, a ylabel, and a title\n",
    "    # Don't forget that the last line needs to be plt.show() so\n",
    "    # that your visualization prints out to the screen."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the code cell below to see the results of your visualization. Your results should look something like this:\n",
    "\n",
    "![Robot 1D Visualization](robot1D.png \"1D Robot Visualization\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZIAAAEWCAYAAABMoxE0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAGiRJREFUeJzt3X+0XWV95/H3x0TALgoKpDM0AYMSx0bUOF4i005pi6JBHcIsoYZShSldqS5ZY8fxB6wZsUW7qjNrhi5b/JHKL1EExFpTGxppAevM+CMXRWJgUi4R5RqmxAYVpUAj3/nj7NTj5ST33Oy77yHc92utvbL3s5/nOc+ji3zy7H3O3qkqJEnaV08Z9QAkSfs3g0SS1IpBIklqxSCRJLVikEiSWjFIJEmtGCSSpFYMEqkDSc5LMp7kkSRX9JWfkOTGJDuT7EjyySRH9p3/3STbkvwgyfYkFydZOJJJSEMySKRubAfeA1w2pf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Fb+P97qzHAcEOSFAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "output_map([.2, .2, .2, .2, .2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 - Updating Probabilities\n",
    "\n",
    "This final problem is meant to be the most challenging one. You'll have to think about how for loops and list indexing work.\n",
    "\n",
    "As the robot moves around and senses its surroundings, it will update its belief about where it is on the map. You'll learn about this in the localization lesson that comes up next. \n",
    "\n",
    "#### Instructons\n",
    "\n",
    "For now, write a function that updates the probabilities for each grid space. The function has these two inputs:\n",
    "\n",
    "* a list containing the probabilities that the robot is at each spot on the map-grid\n",
    "* a list of lists containing the new probabilities. Each element in the list has two entries: the first entry is a point on the map grid, and the second entry is the new probability.\n",
    "\n",
    "#### Example Input and Output\n",
    "Here is some example input:\n",
    "\n",
    "The robot is initialized with a five-grid map, so the probabilities are in a list\n",
    "\n",
    "* `robot_grid = [0.2, 0.2, 0.2, 0.2, 0.2]`\n",
    "\n",
    "The robot figures out that it actually has a 0.4 probability of being at the first spot `robot_grid[0]` and 0.15 probability of being at all the other spots. So the second input looks like this\n",
    "\n",
    "* `updates = [[4, 0.15], [0, .4], [3, 0.15], [1, 0.15], [2, 0.15]]`\n",
    "\n",
    "Notice that this second input is not in the order of the map grid. The first value is for grid 5 (indexed as 4), then grid 1 (indexed as 0), then grid 2 (indexed as 3), etc. \n",
    "\n",
    "The updates variable could also look like this and not contain information about the entire grid:\n",
    "* `updates = [3, 0.1], [4, 0.2]`\n",
    "\n",
    "Therefore, think about how you can use the information in the updates variable to correctly change the values in the robot_grid.\n",
    "\n",
    "The output of the function would be the updated list of probabilities:\n",
    "\n",
    "* [0.4, 0.15, 0.15, 0.15, 0.15]\n",
    "\n",
    "#### Hints\n",
    "\n",
    "Juggling all of this information in your head might prove difficult. Consider taking out a pencil and paper to work through the problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_probabilities(grid, updates):\n",
    "        \n",
    "    ###\n",
    "    # TODO: write a for loop that goes through the updates list \n",
    "    # and replaces the probabilities in the grid variable.\n",
    "    #\n",
    "    # Here are a few HINTS:\n",
    "    #     You can change a value in the grid variable like this:\n",
    "    #         grid[0] = .5  where the 0 represents the first grid space\n",
    "    #\n",
    "    #     To access values in a list of lists, you need two brackets.\n",
    "    #     \n",
    "    #     For example, say \n",
    "    #     updates = [[5, 0.15], [0, .4], [3, 0.15], [1, 0.15], [2, 0.15]]\n",
    "    #\n",
    "    #     updates[0] will give you access to the first element in the list,\n",
    "    #        which  is [5, 0.15].\n",
    "    #\n",
    "    #     updates[0][0] gives you access to the first element of [5, 0.15]\n",
    "    #        or in other words the value 5. updates[0][1] gives you access\n",
    "    #        to the value 0.15.\n",
    "    #    \n",
    "    ###\n",
    "    new_grid = [-1, -1, -1, -1, -1]\n",
    "    if len(grid) == len(updates):\n",
    "        for i in range(len(updates)):\n",
    "           new_grid[updates[i][0]]= updates[i][1]\n",
    "    else: \n",
    "        updates_sum = 0\n",
    "        for i in updates:\n",
    "            updates_sum += i[1]\n",
    "        other_p = round((1 - updates_sum) / (len(grid) - len(updates)), 2)\n",
    "        \n",
    "        for element in updates:\n",
    "            new_grid[element[0]] = element[1]\n",
    "        print(new_grid)\n",
    "        for i in range(len(new_grid)):\n",
    "            print(new_grid[i])\n",
    "            if new_grid[i] == -1:\n",
    "                new_grid = other_p\n",
    "    print(new_grid)\n",
    "\n",
    "    return new_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the code cell below to test your updates_probabilities function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.4, 0.15, 0.15, 0.15, 0.15]\n",
      "[0.4, 0.15, 0.15, 0.15, 0.15]\n",
      "[0.25, -1, -1, -1, 0.15]\n",
      "0.25\n",
      "-1\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'float' object has no attribute '__getitem__'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-53-5579f337db99>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mupdate_probabilities\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m.4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0.4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mupdate_probabilities\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m.4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0.4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mupdate_probabilities\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m.25\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0.25\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.15\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Everything looks good!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-52-d028c8454d5c>\u001b[0m in \u001b[0;36mupdate_probabilities\u001b[1;34m(grid, updates)\u001b[0m\n\u001b[0;32m     36\u001b[0m         \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_grid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_grid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 38\u001b[1;33m             \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_grid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     39\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mnew_grid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m                 \u001b[0mnew_grid\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mother_p\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'float' object has no attribute '__getitem__'"
     ]
    }
   ],
   "source": [
    "assert update_probabilities([0.2, 0.2, 0.2, 0.2, 0.2], [[0, .4], [1, 0.15], [2, 0.15], [3, 0.15], [4, 0.15]]) == [0.4, 0.15, 0.15, 0.15, 0.15]\n",
    "assert update_probabilities([0.2, 0.2, 0.2, 0.2, 0.2], [[1, 0.15], [0, .4], [4, 0.15], [2, 0.15], [3, 0.15]]) == [0.4, 0.15, 0.15, 0.15, 0.15]\n",
    "assert update_probabilities([0.2, 0.2, 0.2, 0.2, 0.2], [[0, .25], [4, 0.15]]) == [0.25, 0.2, 0.2, 0.2, 0.15]\n",
    "\n",
    "print('Everything looks good!')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
